package com.rmb.white.controller;

import com.rmb.common.model.response.CommonCode;
import com.rmb.common.model.response.ResponseResult;
import com.rmb.mart.entity.white.IpWhitelist;
import com.rmb.white.service.IpWhitelistService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName IpWhitelistController
 * @Description 白名单增删改查
 * @Author yxs
 * @Date 2021/1/25 9:23
 **/
@RestController
@RequestMapping("/ipWhitelist")
public class IpWhitelistController {
    /**
     * 服务对象
     */
    @Resource
    private IpWhitelistService ipWhitelistService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne/{id}")
    public IpWhitelist selectOne(@PathVariable("id") Long id) {
        return this.ipWhitelistService.queryById(id);
    }

    /**
     * 分页查询所有
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping("/findAll/{offset}/{limit}")
    public List findAll(@PathVariable("offset") int offset,
                        @PathVariable("limit") int limit){
        return ipWhitelistService.queryAllByLimit(offset, limit);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/selectAll")
    public List selectAll(){
        return ipWhitelistService.findAll();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public ResponseResult delete(@PathVariable("id") Long id){
        boolean b = ipWhitelistService.deleteById(id);

        if (b){
            return new ResponseResult(CommonCode.DELETEED_SUCCESSFULLY);
        }else{
            return new ResponseResult(CommonCode.FAIL);
        }

    }

    /**
     * 通过主键改变状态为开启
     * @param id 主键
     * @return 影响行数
     */
    @PutMapping("/startup/{id}")
    public ResponseResult startup(@PathVariable("id") Long id){

        if (ipWhitelistService.startup(id)> 0){
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 通过主键改变状态为开启
     * @param id 主键
     * @return 影响行数
     */
    @PutMapping("/disable/{id}")
    public ResponseResult disable(@PathVariable("id") Long id){

        if (ipWhitelistService.disable(id)>0){
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 添加
     * @param ipWhitelist
     * @return
     */
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody IpWhitelist ipWhitelist){
        ipWhitelistService.insert(ipWhitelist);
        return new ResponseResult(CommonCode.ADDED_SUCCESSFULLY);
    }

    /**
     * 根据主键修改数据
     * @param ipWhitelist
     * @return
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody IpWhitelist ipWhitelist){
        ipWhitelistService.update(ipWhitelist);
        return new ResponseResult(CommonCode.UPDATEED_SUCCESSFULLY);
    }

}